﻿// Decompiled with JetBrains decompiler
// Type: Netick.DefaultAllocator
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#nullable disable
namespace Netick;

public class DefaultAllocator : IMemoryAllocator
{
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public unsafe void* Malloc(long size, int alignment = 8)
  {
    return Marshal.AllocHGlobal((int) size).ToPointer();
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public unsafe void Free(void* ptr) => Marshal.FreeHGlobal(new IntPtr(ptr));

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public unsafe void Clear(void* ptr, long sizeBytes)
  {
    byte* numPtr = (byte*) ptr;
    for (int index = 0; (long) index < sizeBytes; ++index)
      numPtr[index] = (byte) 0;
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public unsafe void Copy(void* dest, void* source, long sizeBytes)
  {
    byte* numPtr1 = (byte*) dest;
    byte* numPtr2 = (byte*) source;
    for (int index = 0; (long) index < sizeBytes; ++index)
      numPtr1[index] = numPtr2[index];
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public unsafe bool Cmp(void* ptr1, void* ptr2, long sizeBytes)
  {
    byte* numPtr1 = (byte*) ptr1;
    byte* numPtr2 = (byte*) ptr2;
    for (int index = 0; (long) index < sizeBytes; ++index)
    {
      if ((int) numPtr1[index] != (int) numPtr2[index])
        return false;
    }
    return true;
  }
}
